<?php 
	define(SEPARATE, '/');
	echo '<div class="floattop"><div class="itemtitle"><h3>banner广告图</h3><ul class="tab1">';
	if(empty($operation) || $operation=='add'){
		echo '<li class="current"><a href="admin.php?action=bannerpic&operation=add" style="text-decoration:none;"><span style="color:#666;">上传</span></a></li><li><a href="admin.php?action=bannerpic&operation=manage" style="text-decoration:none;"><span style="color:#666;">管理</span></a></li>';
	} elseif($operation=='manage'){
		echo '<li><a href="admin.php?action=bannerpic&operation=add" style="text-decoration:none;"><span style="color:#666;">上传</span></a></li><li class="current"><a href="admin.php?action=bannerpic&operation=manage" style="text-decoration:none;"><span style="color:#666;">管理</span></a></li>';
	}
	echo '</ul></div></div>';

	if($operation=='add'){
		echo '<form method="post" id="bannerpic" action="admin.php?action=bannerpic&operation=set" enctype="multipart/form-data">';
		echo '<input type="file" name="filename"><br>';
		echo '<input type="submit" class="btn" id="submit_cardsubmit" name="cardsubmit" title="按 Enter 键可随时提交您的修改" value="提交">';
	}

	if($operation=='set'){
		if(empty($_FILES['filename']['name'])){//请选择上传文件
			echo '<script type="text/Javascript">alert("请选择上传文件!");window.history.back();</script>';
		}else{
			$fileInfo = pathinfo($_FILES['filename']['name']);
			$_FILES['filename']['name'] = time().rand(100,999).'.'.$fileInfo['extension'];	
			if($fileInfo['extension']=='exe'){//非法操作
				echo '<script type="text/Javascript">alert("上传非法文件!");window.history.back();</script>';
			}
			$new_name = getcwd().SEPARATE.'static'.SEPARATE.'image'.SEPARATE.'bannerpic'.SEPARATE.$_FILES['filename']['name'];
			$re = move_uploaded_file($_FILES['filename']['tmp_name'], $new_name);
			$pic_path = 'static'.SEPARATE.'image'.SEPARATE.'bannerpic'.SEPARATE.$_FILES['filename']['name'];//文件路径,存库用
			C::t('home_bannerpic')->add_bannerpic($pic_path,$_FILES['filename']['name']);
			echo '<script type="text/Javascript">alert("上传图片成功!");';
			echo 'window.location="'.ADMINSCRIPT.'?action=bannerpic&operation=manage";';
			echo '</script>';
		}
	} elseif($operation=='manage' || empty($operation)){
		if(empty($_POST)){
			$re = C::t('home_bannerpic')->get_all_pic();
			if(!empty($re)){
				echo '<form action="admin.php?action=bannerpic&operation=manage" method="post">';
				echo '<ul>';
				foreach($re as $val){
					echo '<li>';
					echo '<img src="'.$val['img_path'].'" width="60" height="60">'.$val['img_name'];
					echo '<input type="radio" name="val['.$val['pid'].'][show]" value="0">不显示';
					echo '<input type="radio" name="val['.$val['pid'].'][show]" value="1">显示';
					echo '<input type="radio" name="val['.$val['pid'].'][show]" value="del">删除';
					echo '&nbsp;&nbsp;&nbsp;&nbsp;';
					if(empty($val['img_url'])){
						echo '链接地址: <input type="text" name="val['.$val['pid'].'][url]" value="http://">';
					}else{
						echo '链接地址: <input type="text" name="val['.$val['pid'].'][url]" value="'. $val['img_url'].'">';
					}
					
					echo '</li>';
				}
				echo '</ul>';
				echo '<input type="submit" value="提交">';
				echo '</form>';
			}	
		}else{
			foreach ($_POST['val'] as $key => $val) {
				C::t('home_bannerpic')->update_pic($key,$val['show'],$val['url']);
			}
			echo '<script type="text/Javascript">alert("操作成功!");window.location.reload();</script>';
		}
	}

?>